package com.ehotting.mqbd.core.manager;

import com.ehotting.eaf.core.StringHelper;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import com.ehotting.mqbd.api.bean.solver.ModuleInfoBean;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.mqbd.api.enums.SolveStepEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.helper.MQStringHelper;
import com.ehotting.mqbd.core.helper.RocketMQAdminHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MqQueueManager {

    private static final Logger logger = LoggerFactory.getLogger(MqQueueManager.class);

    @Autowired
    private RocketMQAdminHelper rocketMQAdminHelper;
    @Autowired
    private MsgStateManager msgStateManager;
    @Autowired
    private ModuleManager moduleInfoManager;
    @Autowired
    private SolverStateManager solverStateManager;
    @Autowired
    private AgentLinkManager agentLinkManager;



    public long getQueueSize(List<MqApplicationContext> mqApplicationContextList){
        int count = 0;
        for(MqApplicationContext mqApplicationContext : mqApplicationContextList){
            count += getQueueSize( mqApplicationContext);
        }
        return count;
    }

    public long getQueueSize(List<MqApplicationContext> mqApplicationContextList, SolverStateBean solverState){
        int count = 0;
        for(MqApplicationContext mqApplicationContext : mqApplicationContextList){
            count += getQueueSize( mqApplicationContext,solverState);
        }
        return count;
    }


    public long getQueueSize(MqApplicationContext mqApplicationContext){
        String topic = MQStringHelper.createTopic(TopicEnum.BLOCK,mqApplicationContext.getModule(), DirectionEnum.REQUEST);
        String consumerGroup = MQStringHelper.createConsumerGroupAtProducerApp(TopicEnum.BLOCK,mqApplicationContext, DirectionEnum.REQUEST);
        return getQueueSize(topic,consumerGroup);
    }

    private long getQueueSize(MqApplicationContext mqApplicationContext, SolverStateBean solverState){
        String topic = MQStringHelper.createTopic(TopicEnum.BLOCK,mqApplicationContext.getModule(), DirectionEnum.REQUEST);
        String consumerGroup = MQStringHelper.createConsumerGroupAtProducerApp(TopicEnum.BLOCK,mqApplicationContext,DirectionEnum.REQUEST,solverState.getSolverInfo().getIp());
        return getQueueSize(topic,consumerGroup);
    }

    private long getQueueSize(String topic,String consumerGroup){
        long count = rocketMQAdminHelper.getAllQueueSize(topic,consumerGroup);
        logger.info("__getQueueSize():topic:{},consumerGroup:{},count:{}",topic,consumerGroup,count);
        return count;
    }


//    public long getQueueIndex(MqApplicationContext mqApplicationContext,String solverIp,String agentId){
//        logger.info("Start getQueueIndexByAgentId");
//
//        logger.info("module:{},consumerHost:{},solverIp:{},agentId:{}",
//                mqApplicationContext.getModule(),
//                mqApplicationContext.getConsumerHost(),
//                solverIp,
//                agentId);
//
//        String msgId = msgStateManager.getMsgIdByAgentIdAndInQueue(agentId);
//        logger.info("getByMsgId msgId:{} by agentId:{}",msgId, agentId);
//        if(StringHelper.isBlank(msgId)){
//            return -1;
//        }
//
//        return getQueueIndexByMsgId(mqApplicationContext,solverIp,msgId);
//
//    }
//
//
//    public long getQueueIndexByMsgId(MqApplicationContext mqApplicationContext,String solverIp,String msgId){
//        logger.info("Start getQueueIndexByAgentId");
//
//        logger.info("module:{},consumerHost:{},solverIp:{}",
//                mqApplicationContext.getModule(),
//                mqApplicationContext.getConsumerHost(),
//                solverIp);
//
//        String topic = MQStringHelper.createTopic(TopicEnum.BLOCK,mqApplicationContext.getModule(), DirectionEnum.REQUEST);
//        String consumerGroup = MQStringHelper.createConsumerGroupAtProducerApp(TopicEnum.BLOCK,mqApplicationContext,DirectionEnum.REQUEST,solverIp);
//        long index = rocketMQAdminHelper.getQueueIndex(topic,consumerGroup,msgId);
//        logger.info("getQueueIndexByMsgId:topic:{},consumerGroup:{},msgId:{},index:{}",topic,consumerGroup,msgId,index);
//        logger.info("Finish getQueueIndexByAgentId");
//        return index;
//    }
//
//    public long getQueueIndex(MqApplicationContext mqApplicationContext,String agentId){
//        String msgId = msgStateManager.getMsgIdByAgentIdAndInQueue(agentId);
//        if(StringHelper.isBlank(msgId)){
//            return -1;
//        }
//        String topic = MQStringHelper.createTopic(TopicEnum.BLOCK,mqApplicationContext.getModule(), DirectionEnum.REQUEST);
//        String consumerGroup = MQStringHelper.createConsumerGroupAtProducerApp(TopicEnum.BLOCK,mqApplicationContext, DirectionEnum.REQUEST);
//        long index = rocketMQAdminHelper.getQueueIndex(topic,consumerGroup,msgId);
//        return index;
//    }
//
//    public long getQueueIndex(String module,String agentId){
//        MqApplicationContext mqApplicationContext = new MqApplicationContext();
//        mqApplicationContext.setModule(module);
//        mqApplicationContext.setConsumerHost(MQDBConstants.DISPATCHER_HOST);
//
//        String solverIp = "";
//        ModuleInfoBean sysModuleInfo  = moduleInfoManager.getByModule(module);
//        if(sysModuleInfo!=null && SolveStepEnum.MULTI == sysModuleInfo.getSolveStep()){
//            solverIp = solverStateManager.getByModelAndAgentId(module,agentId);
//            String consumerIp = agentLinkManager.getConsumerIpAndAgentId(module, agentId);
//            mqApplicationContext.setConsumerHost(consumerIp);
//        }
//        return this.getQueueIndex(mqApplicationContext,solverIp,agentId);
//    }
//
//    public long getQueueIndexByMsgId(String module,String agentId,String msgId){
//        MqApplicationContext mqApplicationContext = new MqApplicationContext();
//        mqApplicationContext.setModule(module);
//        mqApplicationContext.setConsumerHost(MQDBConstants.DISPATCHER_HOST);
//
//        String solverIp = "";
//        ModuleInfoBean sysModuleInfo  = moduleInfoManager.getByModule(module);
//        if(sysModuleInfo!=null && SolveStepEnum.MULTI == sysModuleInfo.getSolveStep()){
//            solverIp = solverStateManager.getByModelAndAgentId(module,agentId);
//            String consumerIp = agentLinkManager.getConsumerIpAndAgentId(module, agentId);
//            mqApplicationContext.setConsumerHost(consumerIp);
//        }
//        return this.getQueueIndexByMsgId(mqApplicationContext,solverIp,msgId);
//    }



}
